﻿Imports DataAccess
Imports DTO
Imports System.Text
Imports System.Net.NetworkInformation

Public Class BSTerminalSetting
    Dim serverName1 As String

    Public Sub New(ByVal serverName As String)
        'Lay gia tri serverName ben Form
        DATerminalSetting.ConfigServerName(serverName)
    End Sub

    '/******************************************************************/
    '/*             Lay datatable tu DataAccess                        */
    '/******************************************************************/
    Public Function GetAllTerminalSetting() As DataTable
        Return DATerminalSetting.GetAllTerminalSetting()
    End Function
    '/******************************************************************/
    '/*             Lay datatable khi search                       */
    '/******************************************************************/
    Public Function SearchTerminalSetting(ByVal groupID As String) As DataTable
        Return DATerminalSetting.SearchTerminalSetting(groupID)
    End Function
    '/******************************************************************/
    '/*             Sua tbTerminalSetting de view                      */
    '/******************************************************************/
    Public Function GetAndCheckAllTerminalSetting(ByVal groupID As String, ByVal chkStatus As Boolean) As DataTable
        Dim dtTerminalSetting As DataTable = SearchTerminalSetting(groupID)
        Dim dtTerminalSettingView As New DataTable
        'Add Column cho dtTerminalSettingView
        dtTerminalSettingView.Columns.Add("Machine No")
        dtTerminalSettingView.Columns.Add("Usage Type")
        dtTerminalSettingView.Columns.Add("Device")
        dtTerminalSettingView.Columns.Add("IP Address")
        dtTerminalSettingView.Columns.Add("Port")
        dtTerminalSettingView.Columns.Add("Status")
        '
        'Add Row value cho dtTerminalSettingView
        If chkStatus = False Then

            For i As Integer = 0 To dtTerminalSetting.Rows.Count - 1
                dtTerminalSettingView.Rows.Add(New Object() {dtTerminalSetting.Rows(i).Item("MACHINE_NO"),
                                                             GetUsageTypeName(dtTerminalSetting.Rows(i).Item("USE_TYPE")),
                                                             GetDeviceName(dtTerminalSetting.Rows(i).Item("DEVICE")),
                                                             dtTerminalSetting.Rows(i).Item("IP_ADRESS"),
                                                             dtTerminalSetting.Rows(i).Item("NETPORT_NO"),
                                                             ""})
                'CheckIPConnect(dtTerminalSetting.Rows(i).Item("IP_ADRESS"))})
            Next
        Else
            For i As Integer = 0 To dtTerminalSetting.Rows.Count - 1
                dtTerminalSettingView.Rows.Add(New Object() {dtTerminalSetting.Rows(i).Item("MACHINE_NO"),
                                                             GetUsageTypeName(dtTerminalSetting.Rows(i).Item("USE_TYPE")),
                                                             GetDeviceName(dtTerminalSetting.Rows(i).Item("DEVICE")),
                                                             dtTerminalSetting.Rows(i).Item("IP_ADRESS"),
                                                             dtTerminalSetting.Rows(i).Item("NETPORT_NO"),
                                                             CheckIPConnect(dtTerminalSetting.Rows(i).Item("IP_ADRESS"))})
            Next
        End If
        Return dtTerminalSettingView
    End Function

    '/******************************************************************/
    '/*               Function check Connect                           */
    '/******************************************************************/
    Public Function CheckIPConnect(ByVal ipAddress As String) As String
        Try
            Dim MyPing As New System.Net.NetworkInformation.Ping
            Dim Myreply As System.Net.NetworkInformation.PingReply = MyPing.Send(ipAddress, 10, Encoding.ASCII.GetBytes("aaa"), New PingOptions())
            If Myreply.Status = IPStatus.Success Then
                Return "Connect"
            Else
                Return "Disconnect"
            End If
        Catch ex As Exception
            Return "Disconnect"
        End Try
    End Function
    '/******************************************************************/
    '/*               Function lay UsageType Name                      */
    '/******************************************************************/
    Public Function GetUsageTypeName(ByVal usageTypeID As Integer) As String
        If usageTypeID = USETYPE_ONLYUSERREGISTRATION Then
            Return USETYPENAME_ONLYUSERREGISTRATION
        ElseIf usageTypeID = USETYPE_ONLYONDUTY Then
            Return USETYPENAME_ONLYONDUTY
        ElseIf usageTypeID = USETYPE_ONLYOFFDUTY Then
            Return USETYPENAME_ONLYOFFDUTY
        ElseIf usageTypeID = USETYPE_BOTHONOFFDUTY Then
            Return USETYPENAME_BOTHONOFFDUTY
        Else
            Return USETYPENAME_ALL
        End If
    End Function
    '/******************************************************************/
    '/*               Function get Device Name                         */
    '/******************************************************************/
    Public Function GetDeviceName(ByVal deviceID As Integer) As String
        If deviceID = DEVICE_TCPIP Then
            Return DEVICENAME_TCPIP
        Else
            Return DEVICENAME_USB
        End If
    End Function
    '/******************************************************************/
    '/*               Function get UseType ID                          */
    '/******************************************************************/
    Public Function GetUseTypeID(ByVal usageTypeName As String) As Integer
        If usageTypeName = USETYPENAME_ONLYUSERREGISTRATION Then
            Return USETYPE_ONLYUSERREGISTRATION
        ElseIf usageTypeName = USETYPENAME_ONLYONDUTY Then
            Return USETYPE_ONLYONDUTY
        ElseIf usageTypeName = USETYPENAME_ONLYOFFDUTY Then
            Return USETYPE_ONLYOFFDUTY
        ElseIf usageTypeName = USETYPENAME_BOTHONOFFDUTY Then
            Return USETYPE_BOTHONOFFDUTY
        Else
            Return USETYPE_ALL
        End If
    End Function
    '/******************************************************************/
    '/*               Function lay Device ID                         */
    '/******************************************************************/
    Public Function GetDeviceID(ByVal deviceName As String) As Integer
        If deviceName = DEVICENAME_TCPIP Then
            Return DEVICE_TCPIP
        Else
            Return DEVICE_USB
        End If
    End Function
    '/******************************************************************/
    '/*             Delete TerminalSetting                             */
    '/******************************************************************/
    Public Function DeleteTerminalSetting(ByVal machineID As String) As Integer
        Return DATerminalSetting.DeleteTerminalSetting(machineID)
    End Function

    '/******************************************************************/
    '/*             Insert TerminalSetting                             */
    '/******************************************************************/
    Public Function InsertTerminalSetting(ByVal machineNo As Integer, ByVal groupID As Integer, ByVal useType As Integer,
                                          ByVal device As Integer, ByVal ipAddress As String, ByVal netPortNo As Integer,
                                          ByVal status As Integer, ByVal createDateTime As DateTime, ByVal updateDateTime As DateTime) As Integer
        Dim dtoTerminalSetting As New DTOTerminalSetting
        dtoTerminalSetting.MachineNoProperty = machineNo
        dtoTerminalSetting.GroupIdProperty = groupID
        dtoTerminalSetting.UseTypeProperty = useType
        dtoTerminalSetting.DeviceProperty = device
        dtoTerminalSetting.IpAddressProperty = ipAddress
        dtoTerminalSetting.NetPortNoProperty = netPortNo
        dtoTerminalSetting.StatusProperty = status
        dtoTerminalSetting.CreateDateTimeProperty = createDateTime
        dtoTerminalSetting.UpdateDateTimeProperty = updateDateTime
        Return DATerminalSetting.InsertTerminalSetting(dtoTerminalSetting)
    End Function
    '/******************************************************************/
    '/*             Update TerminalSetting                             */
    '/******************************************************************/
    Public Function UpdateTerminalSetting(ByVal machineNo As Integer, ByVal groupID As Integer, ByVal useType As Integer,
                                          ByVal device As Integer, ByVal ipAddress As String, ByVal netPortNo As Integer,
                                          ByVal updateDateTime As DateTime) As Integer
        Dim dtoTerminalSetting As New DTOTerminalSetting
        dtoTerminalSetting.MachineNoProperty = machineNo
        dtoTerminalSetting.GroupIdProperty = groupID
        dtoTerminalSetting.UseTypeProperty = useType
        dtoTerminalSetting.DeviceProperty = device
        dtoTerminalSetting.IpAddressProperty = ipAddress
        dtoTerminalSetting.NetPortNoProperty = netPortNo
        dtoTerminalSetting.UpdateDateTimeProperty = updateDateTime
        Return DATerminalSetting.UpdateTerminalSetting(dtoTerminalSetting)
    End Function

    '/******************************************************************/
    '/*        Check FP terminal register at least one                 */
    '/******************************************************************/
    'Get all data FP terminal of register
    Public Function CheckLeastFPTerminalRegister() As Array

        Dim array(4) As Object
        Dim dtTemp As DataTable = GetAllTerminalSetting()
        For i As Integer = 0 To dtTemp.Rows.Count - 1
            If dtTemp.Rows.Item(i).Item(2) = USETYPE_ALL Or dtTemp.Rows.Item(i).Item(2) = USETYPE_ONLYUSERREGISTRATION Then
                array(0) = dtTemp.Rows.Item(i).Item(0) 'machine No
                array(1) = dtTemp.Rows.Item(i).Item(3) 'device No
                array(2) = dtTemp.Rows.Item(i).Item(4) 'ip Address
                array(3) = dtTemp.Rows.Item(i).Item(5) 'net Port
                Return array
            End If
        Next
        MsgBox("FP registration is not avaliable!")
        array(0) = vbNull
        array(1) = vbNull
        array(2) = vbNull
        array(3) = vbNull
        Return array

    End Function

    '/******************************************************************/
    '/*        Get list FPTerminal infor of machine attendance         */
    '/******************************************************************/

    Public Function GetFPTerminal() As DataTable

        Dim dtFPAttend As New DataTable
        dtFPAttend.Columns.Add("")
        dtFPAttend.Columns.Add("")
        dtFPAttend.Columns.Add("")
        dtFPAttend.Columns.Add("")
        dtFPAttend.Columns.Add("")
        Dim dtTemp As DataTable = GetAllTerminalSetting()
        For i As Integer = 0 To dtTemp.Rows.Count-1
            If dtTemp.Rows.Item(i).Item(2) = USETYPE_ONLYONDUTY Or dtTemp.Rows.Item(i).Item(2) = USETYPE_ONLYOFFDUTY Or dtTemp.Rows.Item(i).Item(2) = USETYPE_BOTHONOFFDUTY Or dtTemp.Rows.Item(i).Item(2) = USETYPE_ALL Then
                dtFPAttend.Rows.Add(dtTemp.Rows.Item(i).Item(0), dtTemp.Rows.Item(i).Item(3), dtTemp.Rows.Item(i).Item(4), dtTemp.Rows.Item(i).Item(5), dtTemp.Rows.Item(i).Item(2))
            End If
        Next
        Return dtFPAttend
    End Function

End Class